package com.sgb.kjwl.viewmodel.transport;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.transport.CarHallEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.GoodsSourceFilterEntity;
import com.sgb.kjwl.model.entity.transport.GoodsSourceLocationEntity;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.utils.LocationUtil;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.machine.CarInfoFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CarsInfoModel extends BaseViewModel {

    private CarInfoFragment fragment;
    private MachineRepertory repertory;
    public ObservableField<String> messageNum = new ObservableField<>("");
    public ObservableField<GoodsSourceFilterEntity> filterEntity = new ObservableField<>(new GoodsSourceFilterEntity());
    public ObservableField<GoodsSourceLocationEntity> sendLocation = new ObservableField<>(new GoodsSourceLocationEntity());
    public ObservableField<GoodsSourceLocationEntity> purposeLocation = new ObservableField<>(new GoodsSourceLocationEntity());
    public ObservableField<GoodsInfoTypeEntity3> sourceType = new ObservableField<>(new GoodsInfoTypeEntity3());

    private int pageSize = 10;
    private int pageNum = 1;

    public CarsInfoModel(CarInfoFragment fragment, MachineRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getGoodsTypeData() {

        String carsTypeString = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CARS_INFO_TYPE, "");
        String carsLengthString = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CARS_INFO_LENGTH, "");
        List<GoodsInfoTypeEntity3> carsType = new Gson().fromJson(carsTypeString, new TypeToken<List<GoodsInfoTypeEntity3>>() {
        }.getType());
        List<GoodsInfoTypeEntity3> carsLengthType = new Gson().fromJson(carsLengthString, new TypeToken<List<GoodsInfoTypeEntity3>>() {
        }.getType());
        fragment.getCarsTypeData(carsType);
        fragment.getCarsLengthData(carsLengthType);

    }


    //货车类型
    public void getCarsType() {
        APIManager.getInstance().getDriverAPI().getCarType("1").enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    fragment.getCarsTypeData(response.body().getData());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CARS_INFO_TYPE, new Gson().toJson(response.body().getData()));
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //货车长度
    public void getCarsLength() {
        APIManager.getInstance().getDriverAPI().getCarLenType("1").enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    fragment.getCarsLengthData(response.body().getData());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CARS_INFO_LENGTH, new Gson().toJson(response.body().getData()));
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //运输方式
    public void getTransType() {
        APIManager.getInstance().getDriverAPI().getTransType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.TRANSPORT_TYPE, new Gson().toJson(response.body().getData()));
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //装载方式
    public void getHandleType() {
        APIManager.getInstance().getDriverAPI().getHandleType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CARS_HANDLING_TYPE, new Gson().toJson(response.body().getData()));
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //付款方式
    public void getPayType() {
        APIManager.getInstance().getDriverAPI().getPayType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PAY_TYPE, new Gson().toJson(response.body().getData()));
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //发票方式
    public void getInvoiceType() {
        APIManager.getInstance().getDriverAPI().getInvoiceType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.INVOICE_TYPE, new Gson().toJson(response.body().getData()));
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }


    public void searchCars(String searchType, String keyWord, boolean isLoadMore) {
        if (TextUtils.isEmpty(searchType) || TextUtils.isEmpty(keyWord)) {
            LogUtil.d("CarListFragModel3", "searchCatogories failed , searchType or keyWord is empty");
            return;
        }
        if (isLoadMore) {
            pageNum++;
        } else {
            pageNum = 1;
        }
        Map map = new HashMap();
        map.put("page", pageNum);
        map.put("limit", pageSize);
        map.put("keyWord", keyWord);
        if(LocationUtil.isOpenLocation(fragment.getActivity())) {
            map.put("longitude", fragment.currentLatLng.longitude);
            map.put("latitude", fragment.currentLatLng.latitude);
        }
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().getCarsBySearch(map).enqueue(new Callback<BaseEntity<ProListEntity<List<CarHallEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<CarHallEntity>>>> call, Response<BaseEntity<ProListEntity<List<CarHallEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null || response.body().getData().getList() == null) {
                        return;
                    }
                    if (fragment != null)
                        fragment.getCarsInfoData(response.body().getData().getList(), response.body().getData().getTotalRow(),isLoadMore);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<CarHallEntity>>>> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getUrl(String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getTeamPriceAPI().getUrlSetting("ownerDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id + "&areaCode=" + repertory.getAreaCode();
                    WebViewX5HostActivity.start(fragment.getActivity(), url);
                    LogUtil.v("carsInfo=", url);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
